package com.yztc.xiami_app.adapter.shopcart;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.bean.shopcart.ShopCart;
import com.yztc.xiami_app.ui.shopcart.interfaces.ICheckChangeListener;

import java.util.List;

/**
 * Created by 周旭 on 2016/11/21.
 * "购物车"界面的Fragment的适配器
 */

public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartAdapter.ShopCartViewHolder> {

    private List<ShopCart> mList;
    private LayoutInflater inflater;
    private Context context;
    private boolean isShowPM;//控制显示"加减数量"的标识
    private int totalPrice; //总价
    private boolean flag = false;
    private ICheckChangeListener listener;

    public ShopCartAdapter(Context context, List<ShopCart> mList,ICheckChangeListener listener) {
        this.mList = mList;
        this.context = context;
        this.listener = listener;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    //是否显示"加减数量"的View
    public void setisShowPM(boolean isShowPM) {
        this.isShowPM = isShowPM;
        notifyDataSetChanged();
    }



    @Override
    public int getItemCount() {
        return mList.size();
    }


    @Override
    public ShopCartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_shop_cart, parent, false);
        return new ShopCartViewHolder(itemView);
    }

    //绑定
    @Override
    public void onBindViewHolder(final ShopCartViewHolder holder, final int position) {
        final ShopCart shopCart = mList.get(position);
        holder.checkStateCheckBox.setChecked(shopCart.getStatus()); //设置选中状态
        //Glide加载商品图片
        Glide.with(context).load(shopCart.getGoodsPic()).into(holder.goodsPicIv);
        holder.goodsNameTv.setText(shopCart.getGoodsName()); //设置商品名称
        holder.goodsPriceTv.setText("¥ " + shopCart.getPrice()); //设置商品的单价
        holder.goodsNumTv.setText("x" + shopCart.getNum()); //设置商品的购买数量
        holder.checkStateCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean isChecked = shopCart.getStatus();
                holder.checkStateCheckBox.setChecked(!isChecked);
                mList.get(position).setStatus(!isChecked); //设置商品对象(Bean)的选中状态
                notifyDataSetChanged();
            }
        });
        //动态监听CheckBox的选中状态，动态计算选中的商品总价
        holder.checkStateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int price = shopCart.getPrice(); //获取选中商品的单价
                int num = shopCart.getNum(); //获取选中商品的数量
                int price2 = price * num; //获取选中的单个商品的总价价

                if(isChecked){
                    flag = true;
                }else {
                    flag = false;
                }
                if(flag){
                    totalPrice += price2;
                }else {
                    totalPrice -= price2;
                }
                //通知UI，重新设置总价
                listener.setTotalPrice(totalPrice);
            }
        });

        if (isShowPM) {
            holder.minusIv.setVisibility(View.VISIBLE); //设置可见
            holder.plusIv.setVisibility(View.VISIBLE);//设置可见
            holder.gooNumTv.setVisibility(View.VISIBLE);//设置可见

        } else {
            holder.minusIv.setVisibility(View.GONE); //设置不可见
            holder.plusIv.setVisibility(View.GONE);//设置不可见
            holder.gooNumTv.setVisibility(View.GONE);//设置不可见
        }
    }


    class ShopCartViewHolder extends RecyclerView.ViewHolder {

        private CheckBox checkStateCheckBox; //选中状态
        private ImageView goodsPicIv; //商品图片
        private TextView goodsNameTv; //商品名称
        private TextView goodsPriceTv; //商品单价
        private TextView goodsNumTv; //单个商品数量
        private ImageView minusIv;//减少商品数量按钮
        private ImageView plusIv;//增加商品数量按钮
        private TextView gooNumTv;//商品的数量

        public ShopCartViewHolder(View itemView) {
            super(itemView);
            checkStateCheckBox = (CheckBox) itemView.findViewById(R.id.shop_cart_is_checked_cb);
            goodsPicIv = (ImageView) itemView.findViewById(R.id.shop_cart_goods_pic_iv);
            goodsNameTv = (TextView) itemView.findViewById(R.id.shop_cart_goods_name_tv);
            goodsPriceTv = (TextView) itemView.findViewById(R.id.shop_cart_goods_price_tv);
            goodsNumTv = (TextView) itemView.findViewById(R.id.shop_cart_goods_num);
            //设置控制商品数量的View为显示状态
            minusIv = (ImageView) itemView.findViewById(R.id.shop_cart_goods_num_minus); //减少商品数量按钮
            plusIv = (ImageView) itemView.findViewById(R.id.shop_cart_goods_num_plus);//增加商品数量按钮
            gooNumTv = (TextView) itemView.findViewById(R.id.shop_cart_goods_num_tv); //商品的数量
        }
    }

}
